//
//  InputTradingPassWordView.m
//  NewPro
//
//  Created by RYX on 2019/4/22.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "InputTradingPassWordView.h"
#import "WLUnitField.h"
#import "SetTradingPassWordViewController.h"

@interface InputTradingPassWordView ()

@property (nonatomic,weak) WLUnitField *uniField;

@end

@implementation InputTradingPassWordView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    UIView  *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 5;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self).offset(21);
        
    }];
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [self addSubview:closeBtn];
    [closeBtn setImage:[UIImage imageNamed:@"closeBtn_01"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(onCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.right.equalTo(self);
        make.bottom.equalTo(bgView.mas_top).offset(-1);
        make.width.mas_equalTo(20);
    }];
    
    UILabel *titleLa = [[UILabel alloc] init];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"请输入交易密码" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]}];
    titleLa.attributedText = string;
    titleLa.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:titleLa];
    [titleLa mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(bgView);
        make.top.mas_equalTo(25);
    }];
    
    WLUnitField *uniField = [[WLUnitField alloc] initWithInputUnitCount:6];
    self.uniField = uniField;
    uniField.secureTextEntry = YES;
    uniField.unitSpace = 3;
    uniField.borderRadius = 1;
    uniField.tintColor = [UIColor colorWithHexString:@"E2E2E2"];
    uniField.trackTintColor = [UIColor colorWithHexString:@"E2E2E2"];
    [uniField sizeToFit];
    [uniField addTarget:self action:@selector(unitFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [bgView addSubview:uniField];
    
    [uniField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(titleLa.mas_bottom).offset(13);
        make.left.mas_equalTo(30);
        make.centerX.equalTo(bgView);
        make.height.mas_equalTo(44);
        
    }];
    
    UIButton *fogetBtn = [[UIButton alloc] init];
    [bgView addSubview:fogetBtn];
    [fogetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [fogetBtn addTarget:self action:@selector(onForgetBtn:) forControlEvents:UIControlEventTouchUpInside];
    [fogetBtn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
    fogetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [fogetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(uniField.mas_right);
        make.top.equalTo(uniField.mas_bottom);
    }];
    
    UILabel *statueLa = [[UILabel alloc] init];
    statueLa.text = @"密码错误";
    statueLa.textColor = UIColor.redColor;
    statueLa.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:statueLa];
    statueLa.alpha = 0;
    
    [statueLa mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(uniField.mas_left);
        make.centerY.equalTo(fogetBtn);
    }];
    
    UIButton *sureBtn = [[UIButton alloc] init];
    [bgView addSubview:sureBtn];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    sureBtn.backgroundColor = BLUECOLOR;
    sureBtn.layer.cornerRadius = 5;
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sureBtn addTarget:self action:@selector(onSureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(bgView).offset(29);
        make.centerX.equalTo(bgView);
        make.bottom.equalTo(bgView).offset(-26);
        make.height.mas_equalTo(40);
    }];
}

- (void)unitFieldEditingChanged:(UITextField *)tf{
    
    NSLog(@"%@",tf.text);
}


#pragma mark - 关闭页面
- (void)onCloseBtn:(UIButton *)sender{
    
    [self endEditing:YES];
    self.uniField.text = nil;
    
    if (self.closeBlock != nil) {
        
        self.closeBlock();
    }
}
- (void)returnCloseBlock:(closeBlock)block{
    
    self.closeBlock = block;
}

#pragma mark - 确定
- (void)onSureBtn:(UIButton *)sender{
    
    if (self.uniField.text.length < 6) {
        
        return;
    }
    
    sender.enabled = NO;
    
    [self endEditing:YES];
    
    if (self.sureBlock != nil) {
        
        self.sureBlock(self.uniField.text,sender);
    }
    
}
- (void)returnSureBlock:(sureBlock)block{
    
    self.sureBlock = block;
}

- (void)setBecomeFirstResponder:(BOOL)becomeFirstResponder{
    
    if (becomeFirstResponder) {
        
        [self.uniField becomeFirstResponder];
    }else{
        
        [self.uniField resignFirstResponder];
    }
    
}

- (void)onForgetBtn:(UIButton *)sender{
    
    SetTradingPassWordViewController *VC = [[SetTradingPassWordViewController alloc] init];
    VC.wherefrom = self.wherefrom;
    [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
}
@end
